<?php

namespace app\index\controller;


use think\Controller;

class Index extends Controller
{

    public function easyinsert()
    {
        return $this->fetch();
    }


    public function chenggong()
    {
        return $this->fetch();
    }

    public function index()
    {
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        if(request()->isPost()){
            $data['name']=input('name');
            $data['area_id']=input('city');
            $data['phone']=input('phone');
            $data['comewhere']=input('comewhere');
            $data['create_time']=time();
            $son=db('son')->where(['area_id'=>$data['area_id'],'status'=>1])->count();

            if($son<1) {
                $data['son_id'] = 0;
                db('order')->insert($data);
                return 1;
            }else if($son==1){
                $soninfo=db('son')->where(['area_id'=>$data['area_id'],'status'=>1])->find();
                if($soninfo['type']==1){
                    $sonid=db('son')->where(['area_id'=>$data['area_id'],'status'=>1])->value('son_id');
                    $count=db('son')->where(['son_id'=>$sonid])->value('order_count');
                    $count=$count+1;
                    $daycount=db('order')->where(['status'=>1,'phone'=>$data['phone'],'son_id'=>$sonid,'create_time'=>['between',[$beginToday,$endToday]]])->count();
                    if($daycount>=1){
                        $data['son_id'] = 0;
                        db('order')->insert($data);
                        return 1;
                    }
                    db('son')->where(['son_id'=>$sonid])->update(['order_count'=>$count]);
                    $data['son_id'] = $sonid;
                    $id=db('order')->insertGetId($data);
                    model('Wechat')->send($id);
                    return 1;
                }else{
                    if($soninfo['fee']<50){
                        $data['son_id'] = 0;
                        db('order')->insert($data);
                        return 1;
                    }
                    $sonid=db('son')->where(['area_id'=>$data['area_id'],'status'=>1])->value('son_id');
                    $daycount=db('order')->where(['status'=>1,'phone'=>$data['phone'],'son_id'=>$sonid,'create_time'=>['between',[$beginToday,$endToday]]])->count();
                    if($daycount>=1){
                        $data['son_id'] = 0;
                        db('order')->insert($data);
                        return 1;
                    }
                    $count=db('son')->where(['son_id'=>$sonid])->value('order_count');
                    $count=$count+1;
                    $fee=$soninfo['fee']-50;
                    db('son')->where(['son_id'=>$sonid])->update(['order_count'=>$count,'fee'=>$fee]);
                    $data['son_id'] = $sonid;
                    $id=db('order')->insertGetId($data);
                    $feemove['son_id']=$soninfo['son_id'];
                    $feemove['order_id']=$id;
                    $feemove['type']=2;
                    $feemove['fee']=50;
                    $feemove['create_time']=time();
                    $feemove['status']='自动分配订单扣除';
                    db('fee_move')->insert($feemove);
                    model('Wechat')->send($id);
                    return 1;
                }

            }
            $admin = db('son')->where(['status' =>1,'area_id'=>$data['area_id']])->select();
            $count = [];
            foreach ($admin as $k => $v) {
                $count[$k]['son_id'] = $v['son_id'];
                $count[$k]['count'] = db('order')->where(['son_id' => $v['son_id']])->count();
            }
            $count = $this->arraySequence($count, 'count');
//            var_dump($count);
//            exit();
            for($i=0;$i<=count($count);$i++){
                $soninfo=db('son')->where(['son_id'=>$count[$i]['son_id']])->find();
                if($soninfo['type']==1){
                    $count=db('son')->where(['son_id'=>$soninfo['son_id']])->value('order_count');
                    $daycount=db('order')->where(['status'=>1,'phone'=>$data['phone'],'son_id'=>$soninfo['son_id'],'create_time'=>['between',[$beginToday,$endToday]]])->count();
                    if($daycount>=1){
                        $data['son_id'] = 0;
                        db('order')->insert($data);
                        return 1;
                    }
                    $count=$count+1;
                    $data['son_id']=$soninfo['son_id'];
                    db('son')->where(['son_id'=>$soninfo['son_id']])->update(['order_count'=>$count]);
                    $id=db('order')->insertGetId($data);
                    model('Wechat')->send($id);
                    return 1;
                }else{
                    if($soninfo['fee']<50){
                    }else{
                        $sonid=$soninfo['son_id'];
                        $count=db('son')->where(['son_id'=>$sonid])->value('order_count');
                        $daycount=db('order')->where(['status'=>1,'phone'=>$data['phone'],'son_id'=>$soninfo['son_id'],'create_time'=>['between',[$beginToday,$endToday]]])->count();
                        if($daycount>=1){
                            $data['son_id'] = 0;
                            db('order')->insert($data);
                            return 1;
                        }
                        $count=$count+1;
                        $fee=$soninfo['fee']-50;
                        db('son')->where(['son_id'=>$sonid])->update(['order_count'=>$count,'fee'=>$fee]);
                        $data['son_id'] = $sonid;
                        $id=db('order')->insertGetId($data);
                        $feemove['son_id']=$soninfo['son_id'];
                        $feemove['order_id']=$id;
                        $feemove['type']=2;
                        $feemove['fee']=50;
                        $feemove['create_time']=time();
                        $feemove['status']='自动分配订单扣除';
                        db('fee_move')->insert($feemove);
                        model('Wechat')->send($id);
                        return 1;
                    }

                }
            }


//            return $this->success('提交成功！稍后会有专人与您练习');
        }
        $list=db('mation')->where(['type'=>1])->select();
        $data=db('system')->where(['system_id'=>2])->value('content');
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function sendmsg()
    {
        $data['name']=input('name');
        $data['area_id']=input('city');
        $data['phone']=input('phone');
        $data['create_time']=time();
        $son=db('son')->where(['area_id'=>$data['area_id'],'status'=>1])->count();
        if($son<1) {
            $data['son_id'] = 0;
            db('order')->insert($data);
            return 1;
        }else if($son==1){
            $sonid=db('son')->where(['area_id'=>$data['area_id'],'status'=>1])->value('son_id');
            $count=db('son')->where(['son_id'=>$sonid])->value('order_count');
            $count=$count+1;
            db('son')->where(['son_id'=>$sonid])->update(['order_count'=>$count]);
            $data['son_id'] = $sonid;
            $id=db('order')->insertGetId($data);
            model('Wechat')->send($id);
            return 1;
        }
        $admin = db('son')->where(['status' =>1,'area_id'=>$data['area_id']])->select();
        $count = [];
        foreach ($admin as $k => $v) {
            $count[$k]['son_id'] = $v['son_id'];
            $count[$k]['count'] = db('order')->where(['son_id' => $v['son_id']])->count();
        }
        $count = $this->arraySequence($count, 'count');
        $data['son_id'] = $count[0]['son_id'];
        $count=db('son')->where(['son_id'=>$data['son_id']])->value('order_count');
        $count=$count+1;
        db('son')->where(['son_id'=>$data['son_id']])->update(['order_count'=>$count]);
        $id=db('order')->insertGetId($data);
        model('Wechat')->send($id);
        return 1;
    }


    /**
     * 二维数组根据字段进行排序
     * @params array $array 需要排序的数组
     * @params string $field 排序的字段
     * @params string $sort 排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
     */
    function arraySequence($array, $field, $sort = 'SORT_ASC')
    {
        $arrSort = array();
        foreach ($array as $uniqid => $row) {
            foreach ($row as $key => $value) {
                $arrSort[$key][$uniqid] = $value;
            }
        }
        array_multisort($arrSort[$field], constant($sort), $array);
        return $array;
    }

}
